package com.northwind.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMQUtil {
    //创建连接工厂对象
    private static ConnectionFactory connectionFactory;
    static {
        //创建连接工厂对象
        connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq的ip
        connectionFactory.setHost("192.168.123.105");
        //设置连接的ip端口
        connectionFactory.setPort(15672);
        //设置连接的虚拟主机
        connectionFactory.setHost("/videochannel");
        //设置连接的用户
        connectionFactory.setUsername("DeepHunter");
        //设置连接的密码
        connectionFactory.setPassword("shiqihao140638");
    }

    //获取连接的方法
    public static Connection getConnection(){
        Connection connection =null;
        try {
            //根据连接工厂获取连接
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
            return connection;
    }

    //关闭连接的方法
    public static void closeConnection(Channel channel,Connection connection){
        try {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
